DIST_DIR = RAILS_ROOT + '/../dist'
APP_DIR = File.join(DIST_DIR, 'app')

desc "Create dist version for kuaing."
task :dist => [:make_dist_dir, :copy_to_dist] do
  # delete RoRED.cfg
  puts "Delete RoRED.cfg..."
  FileUtils.rm Dir.glob(File.join(APP_DIR, 'RoRED.cfg'))
  puts "Successful to delete RoRED.cfg."
  puts "------------------------------------"
  
  puts "Delete SVN files..."
  FileUtils.rm_rf Dir.glob(File.join(APP_DIR, '**', '.svn'))
  puts "Successful to delete SVN files."
  puts "------------------------------------"
  
  puts "Delete windows Thumbs.db..."
  FileUtils.rm Dir.glob(File.join(APP_DIR, '**', 'Thumbs.db'))
  puts "Successful to delete Thumbs.db files."
  puts "------------------------------------"
  
  puts "Delete all logs..."
  FileUtils.rm Dir.glob(File.join(APP_DIR, 'log', '*'))
  puts "Successful to delete all logs."
  puts "------------------------------------"
  
  puts "Delete all cache  pids  sessions  sockets..."
  FileUtils.rm Dir.glob(File.join(APP_DIR, 'tmp', 'cache', '*'))
  FileUtils.rm Dir.glob(File.join(APP_DIR, 'tmp', 'pids', '*'))
  FileUtils.rm Dir.glob(File.join(APP_DIR, 'tmp', 'sessions', '*'))
  FileUtils.rm Dir.glob(File.join(APP_DIR, 'tmp', 'sockets', '*'))
  puts "Successful to delete cache  pids  sessions  sockets."
  puts "------------------------------------"
  
  puts "Delete all store upload images"
  FileUtils.rm Dir.glob(File.join(APP_DIR, 'public', 'images', 'store', 'logo', '*'))
  FileUtils.rm Dir.glob(File.join(APP_DIR, 'public', 'images', 'store', 'store_image', '*'))
  FileUtils.rm Dir.glob(File.join(APP_DIR, 'public', 'images', 'store', 'license', '*'))
  puts "Successful to delete store upload images"
  puts "------------------------------------"
  
  puts "Chmod files"
  FileUtils.chmod_R 0777, Dir.glob(File.join(APP_DIR, 'public'))
  FileUtils.chmod_R 0777, Dir.glob(File.join(APP_DIR, 'public', '**'))
  FileUtils.chmod_R 0777, Dir.glob(File.join(APP_DIR, 'public', '*.*'))
  FileUtils.chmod_R 0777, Dir.glob(File.join(APP_DIR, 'public', '**/*.*'))

  FileUtils.chmod_R 0755, Dir.glob(File.join(APP_DIR, 'config'))
  FileUtils.chmod_R 0755, Dir.glob(File.join(APP_DIR, 'config', '**'))
  FileUtils.chmod_R 0644, Dir.glob(File.join(APP_DIR, 'config', '*.*'))

  FileUtils.chmod_R 0755, Dir.glob(File.join(APP_DIR, 'vendor'))
  FileUtils.chmod_R 0755, Dir.glob(File.join(APP_DIR, 'vendor', '**'))
  FileUtils.chmod_R 0644, Dir.glob(File.join(APP_DIR, 'vendor', '**/*.*'))
  
  FileUtils.chmod_R 0777, Dir.glob(File.join(APP_DIR, 'app'))
  FileUtils.chmod_R 0777, Dir.glob(File.join(APP_DIR, 'app', '**'))
  FileUtils.chmod_R 0777, Dir.glob(File.join(APP_DIR, 'app', '**/*.*'))

  FileUtils.chmod_R 0777, Dir.glob(File.join(APP_DIR, 'tmp'))
  FileUtils.chmod_R 0777, Dir.glob(File.join(APP_DIR, 'tmp', '**'))
  FileUtils.chmod_R 0777, Dir.glob(File.join(APP_DIR, 'tmp', '*.*'))
  FileUtils.chmod_R 0777, Dir.glob(File.join(APP_DIR, 'tmp', '**/*.*'))

  FileUtils.chmod_R 0777, Dir.glob(File.join(APP_DIR, 'log'))
  FileUtils.chmod_R 0777, Dir.glob(File.join(APP_DIR, 'log', '*.*'))
  
  puts "Successful to chmod files"
  puts "------------------------------------"
end

task :make_dist_dir do
  unless File.exist?(DIST_DIR)
    begin
      Dir.mkdir(DIST_DIR, 0775)
    rescue => boom
      puts "Cannot make distribution directory: #{boom.message}"
      exit
    end
  end
  
  `rm -rf #{DIST_DIR}/*`
end

task :copy_to_dist do
  FileUtils.cp_r RAILS_ROOT, File.join(DIST_DIR, "app")
end